//
//  HistoryIncidentHeaderView.m
//  Runone
//
//  Created by runone02 on 16/12/7.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "HistoryIncidentHeaderView.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "Masonry.h"
#import "Define.h"
#import "AppConstants.h"
#import "AppDelegate.h"
#import "PileInfo.h"
#import "CustomAnnotationView.h"
#import "CustomCallOutView.h"
#import "ROPointAnnotation.h"

#import "RightCodeTool.h"
#import "OneConst.h"

@interface HistoryIncidentHeaderView ()<MAMapViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *mapBackGroundView;
@property (weak, nonatomic) IBOutlet MAMapView *mapView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageV;
@property (weak, nonatomic) IBOutlet UILabel *eventTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *pileNOLabel;
@property (weak, nonatomic) IBOutlet UILabel *directionLabel;
@property (weak, nonatomic) IBOutlet UILabel *beginTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel;
@property (weak, nonatomic) IBOutlet UIButton *trafficBtn;

@property (nonatomic,strong)NSMutableArray *annotationsArray;


@end

@implementation HistoryIncidentHeaderView

#pragma    ---------历史事件详情


- (IBAction)trafficBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        _mapView.showTraffic=YES;
    } else {
        _mapView.showTraffic=NO;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor  = [UIColor colorWithRed:244.0/255 green:244.0/255 blue:244.0/255 alpha:1];
    _mapView.delegate     = self;
    _mapView.showsCompass = NO;
    _mapView.showsScale   = NO;
    _iconImageV.image        = [UIImage imageNamed:@"event_accident_icon"];
    [self configTextColor];
}

- (void) configTextColor {
    _eventTypeLabel.textColor = kTableViewCellTextColor;
    _pileNOLabel.textColor    = kTableViewCellTextColor;
    _directionLabel.textColor = kTableViewCellTextColor;
    _beginTimeLabel.textColor = kTableViewCellTextColor;
//    _lastTimeLabel.textColor  = kTableViewCellTextColor;
}

- (void) setCeModel:(CurrentEvent *)ceModel {
    _ceModel = ceModel;
    [self loadEventAnnotation];
}



- (NSArray *) getTheWantCameraDevice:(NSString *)roadDeretion {
    AppDelegate *myDelegate  = (AppDelegate*) [UIApplication sharedApplication].delegate ;
    NSArray *dataSourceArray = myDelegate.AllDeviceBaseArray ;
    NSMutableArray *wantCameraDevice = [[NSMutableArray alloc] init];
    
    for (DeviceBaseInfo *model in dataSourceArray) {
        if (model.DeviceType == DevicesTypeCamera && (model.Direction == [roadDeretion integerValue] || model.Direction == 3)) {
            [wantCameraDevice addObject:model];
        }
    }
    return wantCameraDevice;
}

- (NSArray *) getAllCameraGun {
    AppDelegate *myDelegate  = (AppDelegate*) [UIApplication sharedApplication].delegate ;
    NSArray *dataSourceArray = myDelegate.AllDeviceBaseArray ;
    NSMutableArray *wantCameraDevice = [[NSMutableArray alloc] init];
    
    for (DeviceBaseInfo *model in dataSourceArray) {
        if (model.DeviceType == DevicesTypeCamera ) {
            [wantCameraDevice addObject:model];
        }
    }
    return wantCameraDevice;
}


- (void)setEventModel:(EventDetailModel *)eventModel {
    _eventModel = eventModel;
    
    _eventTypeLabel.text = _eventModel.EventBaseInfo.IncidentTypeName;
    
    NSString *rampStr;
    if (eventModel.EventBaseInfo.IsRamp) {
        rampStr = @"匝道";
    }else {
        rampStr = @"主线";
    }
    _directionLabel.text = [NSString stringWithFormat:@"%@ %@",_eventModel.EventBaseInfo.RoadDerectionName, rampStr];
    
    if ([_ceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
        _pileNOLabel.text    = [NSString stringWithFormat:@"%@+%@",_ceModel.BeginPile,_ceModel.BeginPileDistance];
    } else {
        NSString *LStr       = @"L";
        NSString *beginLKStr = [LStr stringByAppendingString:_ceModel.BeginPile];
//        NSString *endLKStr   = [LStr stringByAppendingString:_ceModel.EndPile];
        _pileNOLabel.text    = [NSString stringWithFormat:@"%@+%@",beginLKStr,_ceModel.BeginPileDistance];
    }
//    _pileNOLabel.text    = [NSString stringWithFormat:@"%@+%ld",_eventModel.EventBaseInfo.BeginPile,(long)_eventModel.EventBaseInfo.BeginPileDistance];
    
    _beginTimeLabel.text      = _eventModel.EventBaseInfo.OccurTime;
    _endTimeLabel.text        = _eventModel.EventBaseInfo.ActualEndTime;
}
#pragma mark - 加载地图事件标注
- (void)loadEventAnnotation { //0.232529--0.300532--11.526650--24.709414--113.471923--326.931946
    
    AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
    NSArray *pileArray =  myDelegate.PirArray;
    NSString *pileStr  = _ceModel.BeginPile;
    for (PileInfo *pileModel in pileArray) {
        if ([pileStr isEqualToString: pileModel.PileNo]) {
            _ceModel.Latitude = [NSString stringWithFormat:@"%f",pileModel.Latitude];
            _ceModel.Longitude =[NSString stringWithFormat:@"%f", pileModel.Longitude];
        }
    }
    ROPointAnnotation *roAnnotation = [[ROPointAnnotation alloc] init];
    float latitude = [_ceModel.Latitude floatValue];
    float longitude = [_ceModel.Longitude floatValue];
    
    MACoordinateSpan  span                  = MACoordinateSpanMake(0.232529, 0.300532);
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region           = region;
    
    [_mapView setCenterCoordinate:centerCoordinate animated:NO];
    _mapView.zoomLevel = 11.214808;
    
    CLLocationCoordinate2D ret = CLLocationCoordinate2DMake(latitude, longitude);
    roAnnotation.coordinate = ret;
    _ceModel.Latitude = [NSString stringWithFormat:@"%f",ret.latitude];
    _ceModel.Longitude = [NSString stringWithFormat:@"%f",ret.longitude];
    roAnnotation.currentEventModel = _ceModel;
    [_mapView addAnnotation:roAnnotation];
    
}

#pragma mark -地图代理
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= NO;       //设置气泡可以弹出，默认为NO
        annotationView.calloutView.hidden = YES;
        annotationView.image = [UIImage imageNamed:@"accident_icon"];// 事故图标
        annotationView.centerOffset = CGPointMake(0, -annotationView.image.size.height / 2);
        return annotationView;
    }
    return nil;
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    CustomAnnotationView *customView = (CustomAnnotationView *)view;
    if (customView.CEModel){
        customView.selected       = NO;
        customView.canShowCallout = NO;
        
    }
   customView.calloutView.hidden = YES; 
}

-(void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"%f--%f--%f--%f--%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta,mapView.zoomLevel,mapView.region.center.latitude,mapView.region.center.longitude, mapView.rotationDegree);
}


@end
